package tt.dz.biz.service.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.math.BigDecimal;
import javax.annotation.Resource;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.user.OnlineAccountDAO;
import tt.dz.biz.dao.user.UserDAOImpl;
import tt.dz.common.BigDecimalUtil;
import tt.dz.common.DzConstants;
import tt.dz.common.DzConstants.entUserAllowOverQuota;
import tt.dz.common.DzConstants.userType;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CardErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.DzErrorCode.UserErrorCode;
import tt.dz.common.LogUtil;
import tt.dz.vo.OnlineAccountVO;
import tt.dz.vo.UserBaseVO;

@Service("rpcEntUserCheckQuotaService")
public class RPCEntUserCheckQuotaService
  implements RPCBaseService
{
  @Resource(name="userDAO")
  private UserDAOImpl userDAO;
  @Resource(name="onlineAccountDAO")
  private OnlineAccountDAO onlineAccountDAO;
  
  public Object execute(Protocol.Message message, Object request, Protocol protocol)
    throws Exception
  {
    JSONObject result = new JSONObject();
    Object reqJson = ((GenericRecord)request).get("req");
    try
    {
      JSONObject jsonObj = JSON.parseObject(reqJson.toString());
      String userId = jsonObj.getString("userId");
      String cardNo = jsonObj.getString("cardNo");
      if ((StringUtils.isBlank(userId)) && (StringUtils.isBlank(cardNo)))
      {
        result.put("isSuccess", Integer.valueOf(0));
        result.put("errorCode", DzErrorCode.UserErrorCode.USERID_OR_CARDNO_IS_NULL.value());
        result.put("errorMsg", DzErrorCode.UserErrorCode.USERID_OR_CARDNO_IS_NULL.getDesc());
        return result.toJSONString();
      }
      OnlineAccountVO oacount = null;
      if (StringUtils.isNotBlank(cardNo))
      {
        oacount = this.onlineAccountDAO.queryOnlineCardByNo(cardNo);
        if (oacount == null)
        {
          result.put("isSuccess", Integer.valueOf(0));
          result.put("errorCode", DzErrorCode.CardErrorCode.NOT_FOUND.value());
          result.put("errorMsg", DzErrorCode.CardErrorCode.NOT_FOUND.getDesc());
          return result.toJSONString();
        }
        userId = oacount.getUserId();
      }
      UserBaseVO user = this.userDAO.getUserById(userId);
      if (null == user)
      {
        result.put("isSuccess", Integer.valueOf(0));
        result.put("errorCode", DzErrorCode.UserErrorCode.USER_NOT_FOUND.value());
        result.put("errorMsg", DzErrorCode.UserErrorCode.USER_NOT_FOUND.getDesc());
        return result.toJSONString();
      }
      if (!DzConstants.userType.ENTERPRISE_CUSTOMER.value().equals(user.getUserType()))
      {
        result.put("isSuccess", Integer.valueOf(0));
        result.put("errorCode", DzErrorCode.UserErrorCode.IS_NOT_ENTUSER.value());
        result.put("errorMsg", DzErrorCode.UserErrorCode.IS_NOT_ENTUSER.getDesc());
        return result.toJSONString();
      }
      if (null == user.getBalance()) {
        user.setBalance(BigDecimal.valueOf(0L));
      }
      if ((BigDecimalUtil.compareTo(BigDecimal.valueOf(0L), user.getBalance()) > 0) && 
        (DzConstants.entUserAllowOverQuota.NO.value().equals(user.getAllowOverQuota())))
      {
        result.put("isSuccess", Integer.valueOf(1));
        result.put("quotaFlag", Integer.valueOf(0));
        return result.toJSONString();
      }
      result.put("isSuccess", Integer.valueOf(1));
      result.put("quotaFlag", Integer.valueOf(1));
      return result.toJSONString();
    }
    catch (Exception e)
    {
      LogUtil.error("企业客户检查额度异常，参数为reqJson：" + reqJson.toString(), e);
      result.put("isSuccess", Integer.valueOf(0));
      result.put("errorCode", DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value());
      result.put("errorMsg", DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.getDesc());
    }
    return result.toJSONString();
  }
}
